﻿ 
namespace TL_Cloud.Utils
{
    /// <summary>
    /// zip文件的操作
    /// </summary>
    public class ZipHelper : RebexBase
    {
        string password = "";
        /// <summary>
        /// zip文件操作
        /// </summary>
        public ZipHelper(string password)
        {
            this.password = password;
        }
        Rebex.IO.Compression.ZipArchive zips = null;
        ~ZipHelper()
        {
            if(zips!=null)
            {
                zips.Close();
            }
        }
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="zipFile">zip文件</param>
        /// <param name="CompressionLevel">压缩等级 0~9</param>
        /// <param name="path">文件和路径</param>
        public void Compress(string zipFile,string path,int CompressionLevel=6)
        {
            zips = new Rebex.IO.Compression.ZipArchive(zipFile);
            zips.Password = password;
            if(CompressionLevel<=0)
            {
                CompressionLevel = 0;
            }
            else if(CompressionLevel>=9)
            {
                CompressionLevel = 9;
            }
            zips.CompressionLevel = CompressionLevel;
            zips.CompressionMethod = Rebex.IO.Compression.CompressionMethod.Deflate;
            zips.EncryptionAlgorithm = Rebex.IO.Compression.EncryptionAlgorithm.Aes192;
            zips.Add(path);  

        }
        /// <summary>
        /// 解压缩
        /// </summary>
        public void Decompress(string zipFile,string targetDirectoryPath)
        {
            zips = new Rebex.IO.Compression.ZipArchive(zipFile);
            zips.Password = password;            
            zips.EncryptionAlgorithm = Rebex.IO.Compression.EncryptionAlgorithm.Aes192;
            zips.Extract(zipFile, targetDirectoryPath);
        }
    }
}
